//验证某个值是否为空
function isNullValue(data) {
	if (data == "" || data == null) {
		return false;
	}
	return true;
}

// 验证某个日期是否为YYYY-MM-DD
function isEffectDate(data) {
	var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})/
	if (!reg.test(data)) {
		return false;
	}
	return true;
}

// 验证用户名是否合法
function isEffectUserName(username) {
	var str = username;
	// 在JavaScript中，正则表达式只能使用"/"开头和结束，不能使用双引号
	var Expression = /^(\w){2,12}$/;
	var objExp = new RegExp(Expression);
	if (!objExp.test(str)) {
		return false;
	}
	return true;
}

// 验证密码是否合法
function isEffectPassword(data) {
	var reg = /([w]){6,15}$/;
	if (data == null||data=="" ||!reg.test(data)) {
		return false;
	}
	return true;
}

// 验证两次输入的密码是否一至
function isEffectPasswordEqual(data1, data2) {
	if (data1 != data2) {
		return false;
	}
	return true;
}

//验证输入的文字是否为中文
function isChineseData(data) {
	var reg = /[\u4E00-\u9FA5]/g;
	if (!reg.test(data)) {
		return false;
	}
	if (data ==""&& data == null) {
		return false;
	}
	return true;
}

// 验证手机号码
function isMobile(data) {
	var reg = /^(13[0-9]|15[0-9]|18[0-9])\d{8}$/;
	if (!reg.test(data)) {
		return false;
	}
	return true;
}

// 验证邮箱地址
function isEmail(data) {
	var reg = /^[a-zA-Z0-9_\-]{1,}@[a-zA-Z0-9_\-]{1,}\.[a-zA-Z0-9_\-.]{1,}$/;
	if (!reg.test(data)) {
		return false;
	}
	return true;
}

// 验证身份证号码
function isIdCard(data) {
	var reg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/;
	if (!reg.test(data)) {
		return false;
	}
	return true;
}

/* 
	用途：检查输入字符串是否符合正整数格式 
	输入：s：字符串 
	返回：如果通过验证返回true,否则返回false
*/
function isNumber(data)
{
    var regu = "^[0-9]+$";
    var re = new RegExp(regu);
    //alert(data);
    if(data!=null&&data!=""){
    	if (data.search(re) != - 1) {
            return true;
        }else {
            return false;
        }
    }else{
    	return false;
    }
}

/* 
用途：检查输入字符串是否只由英文字母和数字组成 
输入：s：字符串 
返回：如果通过验证返回true,否则返回false 
*/
function isNumberOrLetter(data)
{
    //判断是否是数字或字母 
    var regu = "^[0-9a-zA-Z]+$";
    var re = new RegExp(regu);
    if (re.test(data)) {
        return true;
    }else {
        return false;
    }
}

//验证单选按钮是否有被中
function isRadioChecked(data) {
	for (var i = 0; i < data.length; i++) {
		if (data[i].checked == true) {
			return true;
			break;
		}
	}
	return false;
}

// 鼠标放上去清除验证提示的内容
// 适合文本框和下拉框
function clearConfirm(dataid) {
	$("." + dataid).html("");
}

// 适合单选
function clearConfirmForRadio(dataid) {
	$("#" + dataid).html("");
}

// 全选反选，传被 选的复先框的的name传过来
function SelectAll(data) {
	var checkboxs = document.getElementsByName(data);
	for (var i = 0; i < checkboxs.length; i++) {
		var e = checkboxs[i];
		e.checked = !e.checked;
	}
}

/**
  * commomSubmit提交表单
  * TODO(这里描述这个方法适用条件 – 可选)
  * @Title: commomSubmit
  * @Description:   TODO
  * @param @param formName  设定文件
  * @return void  返回类型
  * @throws
 */
function commomSubmit(formId){
	var jsonData = $("#"+formId).form2Json();
	var action   = $("#"+formId).attr("action");
	$.post(action,jsonData, function(data) {});
}

$.fn.form2Json = function() {
	var formId = "#" + $(this).attr("id");
	var json = "";
	var radios = "";
	var checks = "";
	var texts = "";
	var combos = "";
	var textareas = "";
	var selector = formId + " input";// 找到表单中所有的input标签
	var comboSelector = formId + " select"; //找到表单中所有select combo对象
	var textareaSelector = formId + " textarea"; //找到表单的大文本输入框
	// 处理大文本框
	for (i = 0; i < $(textareaSelector).length; i++) {
		var textareaelement = textareaSelector + ":eq(" + i + ")";
		var textareaname = $(textareaelement).attr("name");
		if (textareas.indexOf(textareaname) == -1) {
			textareas = (textareas.length == 0 ? "" : textareas + ",")
					+ textareaname + ":\""
					+ $("textarea[name='" + textareaname + "']").val() + "\"";
		}
	}
	// 处理下拉列表框
	for (i = 0; i < $(comboSelector).length; i++) {
		var comboelement = comboSelector + ":eq(" + i + ")";
		var comboname = $(comboelement).attr("name");
		if (combos.indexOf(comboname) == -1) {
			combos = (combos.length == 0 ? "" : combos + ",") + comboname
					+ ":\"" + $("select[name='" + comboname + "']").val() + "\"";
		}
	}
	// 处理input对象
	for (i = 0; i < $(selector).length; i++) {
		var element = selector + ":eq(" + i + ")";
		var etype = $(element).attr("type");
		// 处理文本框
		if (etype == "text" || etype == "password"|| etype == "hidden") {
			var tkname = $(element).attr("name");
			if (texts.indexOf(tkname) == -1) {
				texts = (texts.length == 0 ? "" : texts + ",") + tkname + ":\""+ $("input[name='" + tkname + "']").val() + "\"";
			}
		}
		// 处理单选框
		if (etype == "radio") {
			var rkname = $(element).attr("name");
			if (radios.indexOf(rkname) == -1) {
				var checkedcount = $("input[name='" + rkname + "']:checked").length;
				radios = (radios.length == 0 ? "" : radios + ",")+ rkname+ ":\""+ (checkedcount == 0 ? "" : $(
								"input[name='" + rkname + "']:checked").val())+ "\"";
			}
		}
		// 处理多选框
		if (etype == "checkbox") {
			var ckname = $(element).attr("name");
			// alert(ckname);
			// 1\检查checks是否已经此checkbox
			if (checks.indexOf(ckname + ":") == -1) { // 以前没有找到过
				//2\没有，则将其值存入checks
				var tempchecks = "";
				if ($("input[name='" + ckname + "']:checked").length > 0) {
					for (j = 0; j < $("input[name='" + ckname + "']:checked").length; j++) {
						tempchecks = (tempchecks.length == 0 ? "" : tempchecks+"|")
								+ $("input[name='"+ckname+"']:checked:eq("+j+")").val();
					}
				}
				checks = (checks.length == 0 ? "" : checks + ",") + ckname+ ":\"" + tempchecks + "\"";
			}
		}
	}
	json = (json.length == 0 ? "" : json + ",")
			+ (texts.length == 0 ? "" : texts + ",")
			+ (combos.length == 0 ? "" : combos + ",")
			+ (radios.length == 0 ? "" : radios + ",")
			+ (textareas.length == 0 ? "" : textareas + ",")
			+ (checks.length == 0 ? "" : checks + ",")+"";
	if(json.indexOf(",", 0)){
		json = json.substring(0, json.length);
	}
	if(json.indexOf(",", json.length-1)){
		json = json.substring(0, json.length-1);
	}
	json="{"+json+"}"
	var jsonData = eval('('+json+')');
	return jsonData;
}

//动作提示，需要传入提示语
function checkAction(data) {
	if (confirm(data)) {
		return true;
	} else {
		return false;
	}
}

//页面跳转
function sendUrl(url){
   window.location.href=url;
}

//清楚表单里初始内容
function clearDefaultValue(dataId,defaultValue){
    if($("#"+dataId).val()==defaultValue){
        $("#"+dataId).val("");
    }
    $("."+dataId).html("");
}